package com.exteam.common.view.dialog;

import android.app.ProgressDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.exteam.common.R;
import com.exteam.common.util.CommLogger;


/**
 * 提示消息的Dialog
 *
 * @author sean.yanghy
 * @version v1.0
 * @created 2014-07-06 13:56:23
 */
public class MessageDialog {

    private Context ctx;
    private ProgressDialog pd;
    private View dialogView;
    private TextView loadingMessage;
    private ConfirmListener confirmListener;
    private CancelListener cancelListener;

    public static final int MSG_SHOW = 1000;
    public static final int MSG_CLOSE = 3000;
    public static final int STATUS_SUCCESS = 1;
    public static final int STATUS_FAIL = 0;

    public interface ConfirmListener {
        void onConfirmDialog();
    }

    public interface CancelListener {
        void onCancelDialog();
    }

    public MessageDialog(Context ctx) {
        this.ctx = ctx;

        LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(ctx.LAYOUT_INFLATER_SERVICE);
        dialogView = inflater.inflate(R.layout.view_message, null);
        loadingMessage = (TextView) dialogView.findViewById(R.id.loadingMessage);
        dialogView.findViewById(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (pd.isShowing())
                    pd.dismiss();
                if (confirmListener != null) {
                    confirmListener.onConfirmDialog();
                }
            }
        });
        dialogView.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (pd.isShowing())
                    pd.dismiss();
                if (cancelListener != null)
                    cancelListener.onCancelDialog();
            }
        });
        dialogView.findViewById(R.id.iv_cancel).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (pd.isShowing())
                    pd.dismiss();
                if (cancelListener != null)
                    cancelListener.onCancelDialog();
            }
        });
        pd = new ProgressDialog(ctx, R.style.CustomProgressDialog);
        pd.setCanceledOnTouchOutside(false);
    }

    public ProgressDialog getDialog() {
        return pd;
    }

    public void setOnConfirmListener(ConfirmListener confirmListener) {
        this.confirmListener = confirmListener;
    }

    public void setOnCancelListener(CancelListener cancelListener) {
        this.cancelListener = cancelListener;
    }

    public void openShowProgressMessage(int resString) {
        if (pd != null) {
            if (pd.isShowing()) {
                loadingMessage.setText(resString);
            } else {
                pd.show();
                pd.setContentView(dialogView);
                loadingMessage.setText(resString);
            }
        }
    }

    public void setStatus(int status) {
        loadingMessage.setCompoundDrawablesWithIntrinsicBounds(status == STATUS_SUCCESS ? R.drawable.notice_icon : R.drawable.notice_icon, 0, 0, 0);
    }

    public void show(int resString) {
        show(resString, STATUS_FAIL);
    }

    public void show(int resString, int status) {
        CommLogger.d("show.... ");
        if (pd != null) {
            loadingMessage.setCompoundDrawablesWithIntrinsicBounds(status == STATUS_SUCCESS ? R.drawable.notice_icon : R.drawable.notice_icon, 0, 0, 0);
            if (pd.isShowing()) {
                loadingMessage.setText(resString);
                loadingMessage.setVisibility(View.VISIBLE);
                loadingMessage.invalidate();
            } else {
                pd.show();
                pd.setContentView(dialogView);
                loadingMessage.setText(resString);
                loadingMessage.setVisibility(View.VISIBLE);
                loadingMessage.invalidate();
            }
        }
    }

    public void show(String resString) {
        show(resString, STATUS_FAIL);
    }

    public void show(String resString, int status) {
        CommLogger.d("show.... ");
        if (pd != null) {
            loadingMessage.setCompoundDrawablesWithIntrinsicBounds(status == STATUS_SUCCESS ? R.drawable.notice_icon : R.drawable.notice_icon, 0, 0, 0);
            if (pd.isShowing()) {
                loadingMessage.setText(resString);
                loadingMessage.setVisibility(View.VISIBLE);
                loadingMessage.invalidate();
            } else {
                pd.show();
                pd.setContentView(dialogView);
                loadingMessage.setText(resString);
                loadingMessage.setVisibility(View.VISIBLE);
                loadingMessage.invalidate();
            }
        }
    }

    public void dissmis() {
        try {
            if (pd != null && pd.isShowing()) {
                pd.dismiss();
            }
        } catch (Exception e) {

        }
    }

}
